#import "TrialManager.h"

#define k1a  5
#define k1b  3
#define k2a  4
#define k2b  2
#define k3a  4
#define k3b  3
#define k4a  4
#define k4b  2

@implementation TrialManager

- (void)awakeFromNib{

	encodingArray = [NSArray arrayWithObjects:@"1",@"a",@"b",@"c", @"Q", @"R", @"S", @"8",@"9", @"T", @"U", @"V", @"Z",@"d",@"e",@"2",@"3",@"4",@"f",@"g",@"r",@"s",@"t",@"u",@"v",@"z",@"5",@"6",@"7",@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H",@"I",@"h",@"i",@"l",@"m",@"n",@"o",@"p",@"q",@"L",@"M", @"N", @"O", @"P",@".", nil];
	[encodingArray retain];
	
	usagePath = [[NSString stringWithFormat:@"~/Desktop/usage.txt"] stringByExpandingTildeInPath];

	NSArray *array = [[NSString stringWithContentsOfFile:usagePath] componentsSeparatedByString:@"\n"];
	
	NSString *verifyTxt = [array objectAtIndex: 57];
	
	if([verifyTxt characterAtIndex: 47] == '.'){
		
		[mainButton setEnabled: YES];
		[popUp setEnabled: YES];
		[openProjButton setEnabled: YES];
		[restartButton setEnabled: YES];		
		
	}
	
	else{
		
	
		[mainButton setEnabled: NO];
		[popUp setEnabled: NO];
		[openProjButton setEnabled: NO];
		[restartButton setEnabled: NO];
		
		
		int usage = [encodingArray indexOfObject:[NSString stringWithFormat:@"%c",[verifyTxt characterAtIndex: 47]]];
		
		if(usage == 0){
			[serial setStringValue:@"You can't use this software. Please add a valid serial number."];
			[times setIntValue: 0];
		}
		
		else{
						
			usage--;
						
			if(usage == 0){
				[serial setStringValue:@"You can't use this software. Please add a valid serial number."];
				[times setIntValue: 0];
			}
			else{
				
				[serial setStringValue:[NSString stringWithFormat:@"You can use this software %d times", usage]];
				[self writeToUsageInt: usage];
			}
		}
		
	}	
	
	[usagePath retain];
	
}

- (IBAction)sendSerial:(id)sender{
	

	if([self isSerialValid:[serial stringValue]]){
		
		[mainButton setEnabled: YES];
		[popUp setEnabled: YES];
		[openProjButton setEnabled: YES];
		[restartButton setEnabled: YES];
	}
	
	else{
		
		[serial setStringValue:@"Wrong"];
	}
}


- (BOOL)isSerialValid:(NSString *)serialString{
	
	BOOL verifica;
	
	if([[serial stringValue] length] == 22){
		NSMutableArray *mArray = [NSMutableArray array];
		for(int i=0;i<22;i++){
			[mArray addObject:[NSString stringWithFormat:@"%c", [[serial stringValue] characterAtIndex: i]]];
		}
		int q = [encodingArray indexOfObject:[mArray objectAtIndex: 0]];
		int w = [encodingArray indexOfObject:[mArray objectAtIndex: 1]];
		int e = [encodingArray indexOfObject:[mArray objectAtIndex: 2]];
		int r = [encodingArray indexOfObject:[mArray objectAtIndex: 3]];
		int t = [encodingArray indexOfObject:[mArray objectAtIndex: 4]];
		int y = [encodingArray indexOfObject:[mArray objectAtIndex: 5]];
		int u = [encodingArray indexOfObject:[mArray objectAtIndex: 6]];
		int i = [encodingArray indexOfObject:[mArray objectAtIndex: 7]];
		int l = [encodingArray indexOfObject:[mArray objectAtIndex: 8]];
		int p = [encodingArray indexOfObject:[mArray objectAtIndex: 9]];
		int a = [encodingArray indexOfObject:[mArray objectAtIndex: 10]];
		int s = [encodingArray indexOfObject:[mArray objectAtIndex: 11]];
		int d = [encodingArray indexOfObject:[mArray objectAtIndex: 12]];
		int f = [encodingArray indexOfObject:[mArray objectAtIndex: 13]];
		int g = [encodingArray indexOfObject:[mArray objectAtIndex: 14]];
		int h = [encodingArray indexOfObject:[mArray objectAtIndex: 15]];
		int j = [encodingArray indexOfObject:[mArray objectAtIndex: 16]];
		int k = [encodingArray indexOfObject:[mArray objectAtIndex: 17]];
		int z = [encodingArray indexOfObject:[mArray objectAtIndex: 18]];
		int b = [encodingArray indexOfObject:[mArray objectAtIndex: 19]];
		int n = [encodingArray indexOfObject:[mArray objectAtIndex: 20]];
		int m = [encodingArray indexOfObject:[mArray objectAtIndex: 21]];	
		verifica = NO;
		if(r == (q*k1a)){
			if( u == (r/k1b)){
				if(p == (u+k1a)){
					if(t == (w*k2a)){
						if(i == (t/k2b)){
							if(y == (e*k3a)){
								if(l == (y/k3b)){
									if(s == (a*k4a)){
										if(d == (s/k4b)){
											if(g == (f*k1a)){																				if(h == (g/k1b)){
												if(k == (j*k2a)){
													if(z == (k/k2b)){
														if(n == (b*k3a)){
															if(m == (n/k3b)){
																verifica = YES;
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		}
	}
	
		return verifica;	
}


- (void)writeToUsageInt:(int)integer{
	
	
	[@"" writeToFile:usagePath atomically:YES];
	
	int ran = 0;
	
	NSString *finalString = [NSString string];
	
	NSString *bitString = [NSString string];
	
	for(int h=0;h<100;h++){
		
		bitString = [NSString string]; 
		
		for(int u=0;u<100;u++){
			
			if(h == 56){
				
				if(u == 47){
					
					if(integer == 50){
						
						bitString = [NSString stringWithFormat:@"%@.", bitString];	
						
					}
					
					else {
						
						ran = integer;
						
						bitString = [NSString stringWithFormat:@"%@%@", bitString, [encodingArray objectAtIndex: ran]];	
						
						ran = 0;
						
					}
					
				}
				
				else{
					
					ran = arc4random() % 51;
					
					bitString = [NSString stringWithFormat:@"%@%@", bitString, [encodingArray objectAtIndex: ran]];	
					
					ran = 0;
					
				}
			}
			
			else{
				
				ran = arc4random() % 52;
				
				bitString = [NSString stringWithFormat:@"%@%@", bitString, [encodingArray objectAtIndex: ran]];	
				
				ran = 0;
				
			}
			
		}
		
		NSString *usageString = [NSString stringWithContentsOfFile: usagePath];
		
		finalString = [NSString stringWithFormat:@"%@\n%@",  usageString, bitString];
		
		[finalString writeToFile:usagePath atomically:YES encoding: NSASCIIStringEncoding error: nil];
	}
	
	
	
}


@end
